'''
6.今日公交调度车辆柱状图
'''
from pyecharts import options as opts
from pyecharts.charts import Bar
import mysql.connector
from pyecharts.globals import ThemeType

# 创建数据库连接
cnx = mysql.connector.connect(user='root', password='123456',
                              host='localhost', database='bus_view')

# 创建一个游标对象
cursor = cnx.cursor()

# 执行查询

query = ("SELECT bus_num,bus_count FROM bus_count order by  bus_count desc limit 10;")
cursor.execute(query)

# 获取查询结果并转换为你想要的格式
results = []
swapped_data = [[count, num+"路"] for num, count in cursor]
#
# # 打印结果
# for result in results:
#     print(result)

# 关闭游标和连接
cursor.close()
cnx.close()
print("开始绘制公交车数量图...")
bar2 = (
    Bar(opts.InitOpts(theme=ThemeType.DARK,height="300px",width="400px",chart_id='bar2',bg_color="rgba(16, 12, 42,0.8)"))
    .add_dataset(
        source=swapped_data
    )
    .add_yaxis(
        series_name="公交车数",
        y_axis=[],
        encode={"x": "车辆数量", "y": "线路"},
        label_opts=opts.LabelOpts(is_show=False),
    )
    .set_global_opts(
        legend_opts=opts.LegendOpts(pos_right='right'),
        title_opts=opts.TitleOpts(title="今日公交调度车辆",subtitle='Today bus dispatch vehicles',pos_left='center',pos_top='15',title_textstyle_opts=opts.TextStyleOpts(
        font_size=15,  # 标题的字体大小
        font_family='微软雅黑'
    )),
        yaxis_opts=opts.AxisOpts(type_="category"),
        visualmap_opts=opts.VisualMapOpts(
            orient="horizontal",
            pos_left="center",
            min_=1,
            max_=45,
            range_text=["当前公交车辆多", "当前公交车辆少"],
            dimension=0,
            range_color=["rgba(0, 244, 255, 1)", "rgba(0, 77, 167, 1)"],
        ),
    )

)
bar2.render("6.公交车数量图.html")
